package com.smart.community.region.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smart.community.commons.enums.SystemStatusEnum;
import com.smart.community.region.entity.ZoneBuilding;
import com.smart.community.region.mapper.ZoneBuildingMapper;
import com.smart.community.region.service.IZoneBuildingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分区楼栋关联服务实现类
 *
 * @author Wu.Liang
 * @since 2025-01-30
 */
@Slf4j
@Service
public class ZoneBuildingServiceImpl extends ServiceImpl<ZoneBuildingMapper, ZoneBuilding> implements IZoneBuildingService {

    @Override
    public List<ZoneBuilding> getByZoneId(Long zoneId) {
        log.info("根据分区ID查询楼栋关联列表，分区ID: {}", zoneId);
        
        LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZoneBuilding::getZoneId, zoneId)
                   .eq(ZoneBuilding::getStatus, SystemStatusEnum.ENABLED.getCode());
        
        return this.list(queryWrapper);
    }

    @Override
    public List<ZoneBuilding> getByCommunityId(Long communityId) {
        log.info("根据社区ID查询楼栋关联列表，社区ID: {}", communityId);
        
        LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZoneBuilding::getCommunityId, communityId)
                   .eq(ZoneBuilding::getStatus, SystemStatusEnum.ENABLED.getCode());
        
        return this.list(queryWrapper);
    }

    @Override
    public ZoneBuilding getByBuildingId(Long buildingId) {
        log.info("根据楼栋ID查询分区关联，楼栋ID: {}", buildingId);
        
        LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZoneBuilding::getBuildingId, buildingId)
                   .eq(ZoneBuilding::getStatus, SystemStatusEnum.ENABLED.getCode())
                   .last("LIMIT 1");
        
        return this.getOne(queryWrapper);
    }

    @Override
    public int removeByCommunityId(Long communityId) {
        log.info("根据社区ID删除楼栋关联，社区ID: {}", communityId);
        
        LambdaQueryWrapper<ZoneBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZoneBuilding::getCommunityId, communityId);
        
        return this.baseMapper.delete(queryWrapper);
    }
}
